import {message} from 'antd';
import {formatMessage} from './intl';
import {saleOrderTransportType, saleOrderPriceTerms} from '../Enum';
import {groupBy} from 'lodash';

export const saleOrderValidator = data => {
    const emptyErrors = [];
    if(!data || !data.brandId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.brandId',
            defaultMessage: '品牌'
        }));
    if(!data || !data.dealerId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.dealerId',
            defaultMessage: '总代'
        }));
    if(!data || !data.currency)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.currency',
            defaultMessage: '币种'
        }));
    if(!data || !data.tradeMethodId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.tradeMethodId',
            defaultMessage: '贸易方式'
        }));
        
    if(!data || !data.salesOrderPriceTerms)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.salesOrderPriceTerms',
            defaultMessage: '价格条款'
        }));
    if(!data || !data.paymentType)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.paymentType)',
            defaultMessage: '付款方式'
        }));
    if(!data || !data.deliveryTerms)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.deliveryTerms)',
            defaultMessage: '发运方式'
        }));
    if(emptyErrors.length > 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填！'
        })}`);
        return false;
    }
    return true;
};
export const newOrUpdateOrderValidator = (data, details) => {
    //增加判断必填字段
    const emptyErrors = [];
    if((!data || !data.brandId) && data.dataSource === 3)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.brandId',
            defaultMessage: '品牌'
        }));
    if((!data || !data.dealerId) && data.dataSource === 3)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.dealerId',
            defaultMessage: '总代'
        }));
    if((!data || !data.currency) && data.dataSource === 3)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.currency',
            defaultMessage: '币种'
        }));
    if((!data || !data.tradeMethodId) && data.dataSource === 3)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.tradeMethodId',
            defaultMessage: '贸易方式'
        }));
    if((!data || !data.salesOrderPriceTerms) && data.dataSource === 3)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.salesOrderPriceTerms',
            defaultMessage: '价格条款'
        }));
    if((!data || !data.paymentType) && data.dataSource === 3)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.paymentType)',
            defaultMessage: '付款方式'
        }));
    if((!data || !data.deliveryTerms) && data.dataSource === 3)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.deliveryTerms)',
            defaultMessage: '发运方式'
        }));
    if((!data || !data.shippingPortId))
        emptyErrors.push(formatMessage({
            id: 'validator.empty.shippingPortId)',
            defaultMessage: '发运港口'
        }));
    if(!data || !data.destinationPort)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.destinationPort)',
            defaultMessage: '目的港口'
        }));
    if(!data || !data.commodity)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.commodity)',
            defaultMessage: 'PI描述'
        }));
    if(!data || !data.bankId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.bankId)',
            defaultMessage: '银行账号'
        }));
    if((data && data.salesOrderPriceTerms === saleOrderPriceTerms.FCA) && !data.payableAccount)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.payableAccount)',
            defaultMessage: '到付账号'
        }));
    if((data && (data.salesOrderPriceTerms === saleOrderPriceTerms.FOB ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.FCA ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.EXW)) && !data.carrierId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.carrierId)',
            defaultMessage: '指定货代'
        }));
    if(emptyErrors.length > 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填！'
        })}`);
        return false;
    }
    if(details.length <= 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.detailRequired',
            defaultMessage: '清单不能为空'
        })}`);
        return false;
    }
    if(details.some(d => !d.quantity)) {
        message.warning(formatMessage({
            id: 'validator.message.quantity',
            defaultMessage: '数量必填'
        }));
        return false;
    }
    if(details.some(d => !d.price)) {
        message.warning(formatMessage({
            id: 'validator.message.price',
            defaultMessage: '单价必填'
        }));
        return false;
    }
    if((data.salesOrderPriceTerms === saleOrderPriceTerms.CIF ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.CPT ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.CIP ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.CFR) && (data.deliveryTerms === saleOrderTransportType.Container ||
        data.deliveryTerms === saleOrderTransportType.Railway ||
        data.deliveryTerms === saleOrderTransportType['By Sea']) && details.some(d => !d.containerNumber)) {
        message.warning(formatMessage({
            id: 'validator.message.containerNumber',
            defaultMessage: '集装箱数必填'
        }));
        return false;
    }
    if((data.salesOrderPriceTerms === saleOrderPriceTerms.CIF ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.CPT ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.CIP ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.CFR) && (data.deliveryTerms === saleOrderTransportType.Container ||
        data.deliveryTerms === saleOrderTransportType.Railway ||
        data.deliveryTerms === saleOrderTransportType['By Sea'] ||
        data.deliveryTerms === saleOrderTransportType['RO-RO']) && details.some(d => !d.unitShipmentAmount)) {
        message.warning(formatMessage({
            id: 'validator.message.unitShipmentAmount1',
            defaultMessage: '单位运费必填'
        }));
        return false;
    }
    if((data.salesOrderPriceTerms === saleOrderPriceTerms.CIF ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.CPT ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.CIP ||
        data.salesOrderPriceTerms === saleOrderPriceTerms.CFR) && (data.deliveryTerms === saleOrderTransportType.Container ||
        data.deliveryTerms === saleOrderTransportType.Railway ||
        data.deliveryTerms === saleOrderTransportType['By Sea'] ||
        data.deliveryTerms === saleOrderTransportType['RO-RO']) && details.some(d => d.unitShipmentAmount <= 0)) {
        message.warning(formatMessage({
            id: 'validator.message.unitShipmentAmount2',
            defaultMessage: '单位运费不能小于等于0'
        }));
        return false;
    }
    if(groupBy(details, ['productGroupCode', 'productCode']).length > 1) {
        message.warning(formatMessage({
            id: 'validator.message.productGroupCode',
            defaultMessage: '物料组和产品编号不能重复'
        }));
        return false;
    }
    return true;
};

export const confirmValidator = data => {
    //增加判断必填字段
    const emptyErrors = [];
    if(data.attachments.length <= 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.fileRequired',
            defaultMessage: '附件不能为空'
        })}`);
        return false;
    }
    if(!data || !data.confirmIdea)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.confirmIdea',
            defaultMessage: '确认意见'
        }));
    if(emptyErrors.length > 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填！'
        })}`);
        return false;
    }
    return true;
};
